//
//  LLSearchBar.m
//  ECTA
//
//  Created by 李叶夫 on 2018/2/23.
//  Copyright © 2018年 李叶夫. All rights reserved.
//

#import "LLSearchBar.h"

@interface LLSearchBar ()
//{
//    UIEdgeInsets _contentInset;
//    UIEdgeInsets _textFieldContentInset;
//}

@property (assign, nonatomic) BOOL changeFrame;


@end

@implementation LLSearchBar

- (instancetype)init
{
    if (self = [super init])
    {
        self.clearButtonMode = UITextFieldViewModeAlways;
    }
    return self;
}

- (void)setSearchFieldColor:(UIColor *)searchFieldColor
{
    _searchFieldColor = searchFieldColor;
    [self searchBarTextField].backgroundColor = searchFieldColor;
}

- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor
{
    _placeholderTextColor = placeholderTextColor;
    // iOS 13后废弃
//    [[self searchBarTextField] setValue:placeholderTextColor forKeyPath:@"_placeholderLabel.textColor"];
    NSAttributedString *attriString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: placeholderTextColor}];
    [self searchBarTextField].attributedPlaceholder = attriString;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    [self searchBarTextField].textColor = textColor;
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    [self searchBarTextField].font = font;
}

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
//    [self layoutIfNeeded];
//    for (UIView *sub in [self searchBarTextField].subviews)
//    {
//        if ([sub isKindOfClass:NSClassFromString(@"_UISearchBarSearchFieldBackgroundView")])
//        {
//            UIImageView *subImgView = (UIImageView *)sub;
//            subImgView.image = [UIImage lyf_setCornerImageWithSize:CGSizeMake(1, 1) fillColor:[UIColor whiteColor]];
//            if (cornerRadius)
//                subImgView.layer.cornerRadius = cornerRadius;
//            else
//                subImgView.layer.cornerRadius = 0;
//            subImgView.layer.masksToBounds = YES;
//        }
//    }
}

- (void)setSemicircle:(BOOL)semicircle
{
    _semicircle = semicircle;
    [self setNeedsLayout];
}

- (void)setTextFieldContentInset:(UIEdgeInsets)textFieldContentInset
{
    _textFieldContentInset = textFieldContentInset;
    self.changeFrame = YES;
    [self setNeedsLayout];
}

- (void)setClearButtonMode:(UITextFieldViewMode)clearButtonMode
{
    _clearButtonMode = clearButtonMode;
    [self searchBarTextField].clearButtonMode = clearButtonMode;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.changeFrame)
    {
        CGFloat height = self.bounds.size.height;
        CGFloat width = self.bounds.size.width;
        [self searchBarTextField].frame = CGRectMake(_textFieldContentInset.left, _textFieldContentInset.top, width - _textFieldContentInset.left - _textFieldContentInset.right, height - _textFieldContentInset.top - _textFieldContentInset.bottom);
    }
    for (UIView *sub in [self searchBarTextField].subviews)
    {
        if ([sub isKindOfClass:NSClassFromString(@"_UISearchBarSearchFieldBackgroundView")])
        {
            if (_semicircle)
                sub.layer.cornerRadius = sub.frame.size.height/2;
            else
                sub.layer.cornerRadius = 0;
            sub.clipsToBounds = YES;
        }
    }
    
}


- (void)setCancelButtonTitle:(NSString *)cancelButtonTitle
{
    _cancelButtonTitle = cancelButtonTitle;
    UIButton *cancelBtn = [self valueForKeyPath:@"cancelButton"];
    [cancelBtn setTitle:cancelButtonTitle forState:UIControlStateNormal];
    cancelBtn.enabled = YES;
}

- (void)setCancelButtonTitleColor:(UIColor *)cancelButtonTitleColor
{
    _cancelButtonTitleColor = cancelButtonTitleColor;
    UIButton *cancelBtn = [self valueForKeyPath:@"cancelButton"];
    [cancelBtn setTitleColor:cancelButtonTitleColor forState:UIControlStateNormal];
    cancelBtn.enabled = YES;
}

- (void)setCancelButtonTitleFont:(UIFont *)cancelButtonTitleFont
{
    _cancelButtonTitleFont = cancelButtonTitleFont;
    UIButton *cancelBtn = [self valueForKeyPath:@"cancelButton"];
    cancelBtn.titleLabel.font = cancelButtonTitleFont;
    cancelBtn.enabled = YES;
}

- (void)setHeightConstraint:(CGFloat)heightConstraint
{
    _heightConstraint = heightConstraint;
    if (@available(iOS 11.0, *))
    {
        [[self.heightAnchor constraintEqualToConstant:heightConstraint] setActive:YES];
    }
}


- (UITextField *)searchBarTextField
{
    return [self valueForKey:@"searchField"];
}

@end
